package com.app.service.impl;

import com.app.mapper.PersonMapper;
import com.app.model.PersonModel;
import com.app.service.PersonService;
import com.app.util.StringTool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Primary
public class PersonServiceImpl implements PersonService {
    @Autowired
    PersonMapper personMapper;

//    @Override
//    public RoleModel getRoleByRoleId(int id) {
//        QueryWrapper<RoleModel> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("rules","role_name").eq("id", id);
//         RoleModel role =  roleMapper.selectOne(queryWrapper);
//        if (role == null) {
//            return null;
//        } else {
//            return role;
//        }
//    }

    @Override
    public Page<PersonModel> getPagePersonList(Integer page, Integer limit, String name, String positionDesc, List<Integer> idList, List<Integer> ids) {
        QueryWrapper<PersonModel> queryWrapper = new QueryWrapper<>();

        if (StringTool.isNotEmpty(name)) {
            queryWrapper.like("name", name).or().eq("id", name);
        }
        if (StringTool.isNotEmpty(positionDesc)) {
            queryWrapper.like("position_desc", positionDesc);
        }
        if (idList != null&&idList.size()>0) {
            queryWrapper.in("department_id", idList);
        }
        if (ids != null&&ids.size()>0) {
            queryWrapper.in("department_id", ids);
        }

        queryWrapper.ne("status", 0).orderByAsc("sort").orderByDesc("id");
        Page<PersonModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<PersonModel> personList = personMapper.selectPage(pages, queryWrapper);
        return personList;
    }

    @Override
    public Page<PersonModel> getPageTrainList(Integer page, Integer limit, String name, List<Integer> idList) {
        QueryWrapper<PersonModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name", "department_id", "position_desc", "gender", "train_desc");
        if (idList == null || idList.size() <= 0) {
            queryWrapper.select().like("name", name);
        } else {
                queryWrapper.in("department_id", idList);
                queryWrapper.select().like("name", name);
        }
        queryWrapper.ne("status", 0).orderByDesc("id");
        Page<PersonModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<PersonModel> personList = personMapper.selectPage(pages, queryWrapper);
        return personList;
    }

    @Override
    public PersonModel selectPersonById(int id) {
        QueryWrapper<PersonModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name", "department_id", "name", "position_desc", "gender", "nation", "birthday", "native_place", "birth_place", "rd_time", "zzmm", "join_work_time", "is_health", "education", "major", "school", "person_type", "phone", "idcard", "address", "position_level", "rw_time", "position_tec", "rank_title", "status","is_special")
                .eq("id", id)
                .eq("status", 1).orderByAsc("id");
        PersonModel personModel = personMapper.selectOne(queryWrapper);
        if (personModel == null) {
            return null;
        } else {
            return personModel;
        }
    }

    @Override
    public Integer getPersonCount(Integer departmentId) {
        QueryWrapper<PersonModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("department_id", departmentId);
        return personMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer PersonCount(List<Integer> depIdsList) {
        QueryWrapper<PersonModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        if (depIdsList != null&&depIdsList.size()>0) {
            queryWrapper.in("department_id", depIdsList);
        }
        Integer depCount = personMapper.selectCount(queryWrapper);
        return depCount;
    }


    @Override
    public List<PersonModel> listExport() {
        QueryWrapper<PersonModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name", "department_id", "name", "position_desc", "gender", "nation", "birthday", "native_place", "birth_place", "rd_time", "zzmm", "join_work_time", "is_health", "education", "major", "school", "person_type", "phone", "idcard", "address", "position_level", "rw_time", "position_tec", "rank_title");
        List<PersonModel> personModelList = personMapper.selectList(queryWrapper);
        return personModelList;
    }

    @Override
    public List<PersonModel> newListExport(List<String> personList) {
        QueryWrapper<PersonModel> queryWrapper = new QueryWrapper<>();
        for (int i = 0; i < personList.size(); i++) {
            queryWrapper.select().or().eq("id", personList.get(i));
        }
        queryWrapper.orderByAsc("id");
        List<PersonModel> personModelList = personMapper.selectList(queryWrapper);
        return personModelList;
    }

    @Override
    public List<PersonModel> otherPersonList(List<Integer> idList) {
        QueryWrapper<PersonModel> queryWrapper = new QueryWrapper<>();
        if (idList.size() <= 0 || idList == null) {
            queryWrapper.select("id", "name", "department_id", "position_desc", "gender", "train_desc");
        } else {
                queryWrapper.select("id", "name", "department_id", "position_desc", "gender", "train_desc").in("department_id", idList);
        }
        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("id");
        List<PersonModel> personModelList = personMapper.selectList(queryWrapper);
        return personModelList;
    }

    @Override
    public List<PersonModel> getStatisticsByIds(List<Integer> idList,String key) {
        QueryWrapper<PersonModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(key,"COUNT(*) as count");
        if (idList == null && idList.size() <= 0) {
            queryWrapper.select();
        } else {
            for (int i = 0; i < idList.size(); i++) {
                queryWrapper.or().eq("department_id", idList.get(i));
            }
        }
        queryWrapper.ne("status", 0);
        queryWrapper.groupBy(key);
        List<PersonModel> personModelList = personMapper.selectList(queryWrapper);
        return personModelList;
    }

    @Override
    public void update(PersonModel model) {
        personMapper.updateById(model);
    }

    @Override
    public Integer insert(PersonModel model) {
        return personMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        personMapper.deleteById(id);
    }

    /**
     * 得到当前日期的年份
     *
     * @return YYYY
     */
    public static String getCurrentDateOfYear() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
        String times = sdf.format(date);
        return times;
    }

//
//    @Override
//    public void delete(int id) {
//        roleMapper.deleteById(id);
//    }
}
